package com.principalservice.controller;

import annotation.DisableBaseResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.principalservice.service.DeptService;
import com.principalservice.service.UserInfoService;
import generator.domain.Dept;
import generator.domain.UserInfo;
import generator.support.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/dept")
@Slf4j
public class DeptController {

    @Autowired
    DeptService deptService;

    @Autowired
    UserInfoService userInfoService;

    /**
     * 按页获取部门信息，一页十条
     * @return 部门信息
     */
    @GetMapping("/getdept")
    public List<Dept> getAllDept(){
        log.info("DeptController -> getAllDept");
        return deptService.list();
    }

    /**
     * 根据ID删除部门
     * @param id 部门id
     * @return 是否删除成功
     */
    @GetMapping("/delete/{id}")
    @DisableBaseResponse
    public ResponseResult<Object> deleteDeptById(@PathVariable Integer id){
        log.info("DeptController -> deleteDeptById");
        // 查看该部门是否还有员工
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("user_dept_id", id);
        int count = userInfoService.count(userInfoQueryWrapper);
        if (count > 0){
            return new ResponseResult<>(400, "该部门还有员工，无法删除");
        }
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("dept_id", id);
        if (deptService.remove(qw)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "删除失败");
    }

    /**
     * 添加新的部门，负责人需要在员工名单里
     * @param dept 部门信息
     * @return 是否添加成功
     */
    @PostMapping("/addDept")
    @DisableBaseResponse
    public ResponseResult<Dept> addDept(@RequestBody Dept dept){
        log.info("DeptController -> addDept");
        QueryWrapper<UserInfo> userQw = new QueryWrapper<>();
        userQw.eq("user_name", dept.getDeptPrincipal());
        int count = userInfoService.count(userQw);
        if (count == 0){
            return new ResponseResult<>(400, "没有此员工，请检查负责人姓名");
        }
        if (deptService.save(dept)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "添加失败，请检查信息");
    }

    @PostMapping("/editDept")
    @DisableBaseResponse
    public ResponseResult<Dept> editDept(@RequestBody Dept dept){
        log.info("DeptController -> editDept");
        QueryWrapper<UserInfo> userqw = new QueryWrapper<>();
        userqw.eq("user_name", dept.getDeptPrincipal());
        int count = userInfoService.count(userqw);
        if (count == 0){
            return new ResponseResult<>(400, "没有此员工，请检查负责人姓名");
        }
        if (deptService.updateById(dept)){
            return new ResponseResult<>();
        }
        return new ResponseResult<>(400, "修改失败，请检查信息");
    }
    
    @GetMapping("/deptName")
    public List<String> getAllDeptName(){
        List<Dept> list = deptService.list();
        List<String> result = new ArrayList<>();
        for (Dept dept : list) {
            result.add(dept.getDeptName());
        }
        return result;
    }

}
